package com.hsnn.medstgmini.pro.base.controller;

import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.pro.base.service.WsjDelRelationManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.DateUtil;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 卫生局配送关系管理
 * @author wangbing
 * @date 2015年11月20日
 */
@Controller
@RequestMapping("/wsjDelRelation")
public class WsjDelRelationController {
	
	private static final Logger log = Logger.getLogger(WsjDelRelationController.class);
	
	@Autowired
	private WsjDelRelationManager wsjDelRelationManager;
	
	@Autowired
	private HttpSession session;
	
	@Autowired
	private HttpServletRequest request;
	

	/**
	 * @category 获取当前登陆人信息
	 * @auth wangbing
	 * @return
	 * @date 2015-11-13 08:32:31
	 */
	private SysUser getUser() {
		return (SysUser) session.getAttribute(Constants.USERINFO);
	}
	
	/**
	 * 
	 *@category 获取参数打印
	 *@author wangbing
	 *@date 2015年6月9日
	 *Pagination
	 */
	@SuppressWarnings("rawtypes")
	private void printParams(){
		Enumeration paramNames = request.getParameterNames();
		try {
			while (paramNames.hasMoreElements()) {
				String paramName = (String) paramNames.nextElement();
				String paramValue = request.getParameter(paramName);
				log.info(paramName+":"+paramValue);
			}
		} catch (Exception e) {
			log.error("",e);
		}
		
	}
	
	/**
	 * 
	 *@category 跳转wsj区域配送企业申报
	 *@author wangbing
	 *@date 2015年11月19日 下午2:28:07
	 *@param 
	 *@return Pagination
	 */
	@RequestMapping("/toAreaDelCompDeclare")
	public String toAreaDelCompDeclare(){
		return "/pro/base/areaDelCompDeclare";
		
	}
	
	/**
	 * 
	 *@category 获取配送企业列表 给wsj申报
	 *@author wangbing
	 *@date 2015-11-20 10:45:29
	 *@param 
	 *@return Pagination
	 */
	@RequestMapping("/getDelCompList")
	@ResponseBody
	public Pagination getDelCompList(){
		printParams();
		Pagination page = new Pagination(request);
		page.getConditions().put("delCompCode", request.getParameter("delCompCode"));
		page.getConditions().put("delCompName", request.getParameter("delCompName"));
		page.getConditions().put("orgId", getUser().getOrgId());
		try {
			wsjDelRelationManager.getDelCompList(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * 
	 *@category wsj 申报配送企业 
	 *@author wangbing
	 *@date 2015-11-20 10:45:29
	 *@param page
	 *@return Pagination
	 */
	@RequestMapping("/declareDelComp")
	@ResponseBody
	public Pagination declareDelComp(){
		printParams();
		Pagination page = new Pagination();
		String array = request.getParameter("array");
		String updUser = getUser().getId();
		String orgId = getUser().getOrgId();
		String updDatetime = DateUtil.dateTime(new Timestamp(System.currentTimeMillis()));
		try {
			if (StringUtils.isNotBlank(array)){
				Gson gson = new Gson();
				Type type = new TypeToken<List<Map<String,String>>>() {}.getType();
				List<Map<String,String>> list = gson.fromJson(array, type);
				for(Map<String,String> data : list){
					data.put("updUser", updUser);
					data.put("orgId", orgId);
					data.put("updDatetime", updDatetime);
				}
				page.setOperCount(wsjDelRelationManager.declareDelComp(list));
				page.setSuccess(true);
			}else {
				page.setSuccess(false);
			}
		} catch (JsonSyntaxException e) {
			page.setSuccess(false);
			e.printStackTrace();
		}
		
		return page;
	}
	
	/**
	 * 
	 *@category 跳转wsj 遴选结果查看页面
	 *@author wangbing
	 *@date 2015年11月19日 下午2:28:07
	 *@param 
	 *@return Pagination
	 */
	@RequestMapping("/toSelectResultSee")
	public String toSelectResultSee(){
		return "/pro/base/selectResultSee";
		
	}
	
	/**
	 * 
	 *@category wsj 获取遴选结果列表
	 *@author wangbing
	 *@date 2015-11-20 10:45:29
	 *@param 
	 *@return Pagination
	 */
	@RequestMapping("/getSelectResultList")
	@ResponseBody
	public Pagination getSelectResultList(){
		printParams();
		Pagination page = new Pagination(request);
		page.getConditions().put("delCompCode", request.getParameter("delCompCode"));
		page.getConditions().put("delCompName", request.getParameter("delCompName"));
		page.getConditions().put("delRange", request.getParameter("delRange"));
		page.getConditions().put("status", request.getParameter("status"));
		page.getConditions().put("orgId", getUser().getOrgId());
		try {
			wsjDelRelationManager.getSelectResultList(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}

}
